/******************************************************************************
 *                                                                            *
 * Copyright (c) 2013 by TUTK Co.LTD. All Rights Reserved.                    *
 *                                                                            *
 * Author: Chun Chen                                                          *
 * Date: 2013-02-7                                                           *

 ******************************************************************************/

package com.tutk.IOTC;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import android.os.Message;
import android.util.Log;

//import com.tutk.IOTC.iotc_err;
//import com.tutk.IOTC.iotc2;

public class CommApis extends IOTCAPIs {
	private int rc = 0;
	private String str = "";

	public static String verN2Str(long nVer) {
		String strVer = String.format("%d.%d.%d.%d", (nVer >> 24) & 0xff,
				(nVer >> 16) & 0xff, (nVer >> 8) & 0xff, nVer & 0xff);
		return strVer;
	}

	public void SleepMS(int nMsec) {
		try {
			Thread.sleep(nMsec);
		} catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
	}

	public static String ByteToString(byte[] buf) {
		String strRecvMsg = "";
		try {
			buf = subtractBytes(buf);
			strRecvMsg = new String(buf, 0, buf.length, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return strRecvMsg;
	}

	public static String ByteToString(byte[] buf, int len) {
		String strRecvMsg = "";
		try {
			strRecvMsg = new String(buf, 0, len, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return strRecvMsg;
	}

	public void logi(String _str) {
		Log.i("return", _str);
	}
	public static byte[] subtractBytes(byte[] src){
		for (int count = src.length -1;count > 0;count--){
			if (src[count] != 0){
				return Arrays.copyOfRange(src,0,count+1);
			}
		}
		return src;
	}
}
